use ::libc;
extern "C" {
    pub type hyperlinks;
    pub type screen_write_cline;
    pub type options;
    fn calloc(_: u64, _: u64) -> *mut libc::c_void;
    fn free(_: *mut libc::c_void);
    fn memcpy(_: *mut libc::c_void, _: *const libc::c_void, _: u64) -> *mut libc::c_void;
    fn strlen(_: *const i8) -> u64;
    fn strlcat(_: *mut i8, _: *const i8, _: size_t) -> size_t;
    fn xmalloc(_: size_t) -> *mut libc::c_void;
    fn xcalloc(_: size_t, _: size_t) -> *mut libc::c_void;
    fn xstrdup(_: *const i8) -> *mut i8;
    fn options_get_number(_: *mut options, _: *const i8) -> i64;
    static mut global_options: *mut options;
    fn grid_view_clear(_: *mut grid, _: u_int, _: u_int, _: u_int, _: u_int, _: u_int);
    fn grid_view_delete_lines(_: *mut grid, _: u_int, _: u_int, _: u_int);
    fn screen_write_make_list(_: *mut screen);
    fn screen_write_free_list(_: *mut screen);
    fn grid_unwrap_position(_: *mut grid, _: *mut u_int, _: *mut u_int, _: u_int, _: u_int);
    fn grid_wrap_position(_: *mut grid, _: u_int, _: u_int, _: *mut u_int, _: *mut u_int);
    fn grid_reflow(_: *mut grid, _: u_int);
    fn grid_duplicate_lines(_: *mut grid, _: u_int, _: *mut grid, _: u_int, _: u_int);
    fn grid_clear_lines(_: *mut grid, _: u_int, _: u_int, _: u_int);
    fn grid_adjust_lines(_: *mut grid, _: u_int);
    fn grid_destroy(_: *mut grid);
    fn grid_create(_: u_int, _: u_int, _: u_int) -> *mut grid;
    fn grid_empty_line(_: *mut grid, _: u_int, _: u_int);
    fn utf8_copy(_: *mut utf8_data, _: *const utf8_data);
    fn utf8_stravis(
        _: *mut *mut i8,
        _: *const i8,
        _: i32,
    ) -> i32;
    fn utf8_isvalid(_: *const i8) -> i32;
    fn fatal(_: *const i8, _: ...) -> !;
    fn log_debug(_: *const i8, _: ...);
    fn fatalx(_: *const i8, _: ...) -> !;
    fn hyperlinks_init() -> *mut hyperlinks;
    fn hyperlinks_reset(_: *mut hyperlinks);
    fn hyperlinks_free(_: *mut hyperlinks);
}
pub type __u_char = u8;
pub type __u_short = u16;
pub type __u_int = u32;
pub type __time_t = i64;
pub type u_char = __u_char;
pub type u_short = __u_short;
pub type u_int = __u_int;
pub type time_t = __time_t;
pub type size_t = u64;
pub type bitstr_t = u8;
#[derive(Copy, Clone)]
#[repr(C)]
pub struct grid_cell {
    pub data: utf8_data,
    pub attr: u_short,
    pub flags: u_char,
    pub fg: i32,
    pub bg: i32,
    pub us: i32,
    pub link: u_int,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct utf8_data {
    pub data: [u_char; 21],
    pub have: u_char,
    pub size: u_char,
    pub width: u_char,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct screen {
    pub title: *mut i8,
    pub path: *mut i8,
    pub titles: *mut screen_titles,
    pub grid: *mut grid,
    pub cx: u_int,
    pub cy: u_int,
    pub cstyle: screen_cursor_style,
    pub default_cstyle: screen_cursor_style,
    pub ccolour: i32,
    pub default_ccolour: i32,
    pub rupper: u_int,
    pub rlower: u_int,
    pub mode: i32,
    pub default_mode: i32,
    pub saved_cx: u_int,
    pub saved_cy: u_int,
    pub saved_grid: *mut grid,
    pub saved_cell: grid_cell,
    pub saved_flags: i32,
    pub tabs: *mut bitstr_t,
    pub sel: *mut screen_sel,
    pub write_list: *mut screen_write_cline,
    pub hyperlinks: *mut hyperlinks,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct screen_sel {
    pub hidden: i32,
    pub rectangle: i32,
    pub modekeys: i32,
    pub sx: u_int,
    pub sy: u_int,
    pub ex: u_int,
    pub ey: u_int,
    pub cell: grid_cell,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct grid {
    pub flags: i32,
    pub sx: u_int,
    pub sy: u_int,
    pub hscrolled: u_int,
    pub hsize: u_int,
    pub hlimit: u_int,
    pub linedata: *mut grid_line,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct grid_line {
    pub celldata: *mut grid_cell_entry,
    pub cellused: u_int,
    pub cellsize: u_int,
    pub extddata: *mut grid_extd_entry,
    pub extdsize: u_int,
    pub flags: i32,
    pub time: time_t,
}
#[derive(Copy, Clone)]
#[repr(C, packed)]
pub struct grid_extd_entry {
    pub data: utf8_char,
    pub attr: u_short,
    pub flags: u_char,
    pub fg: i32,
    pub bg: i32,
    pub us: i32,
    pub link: u_int,
}
pub type utf8_char = u_int;
#[derive(Copy, Clone)]
#[repr(C, packed)]
pub struct grid_cell_entry {
    pub c2rust_unnamed: C2RustUnnamed,
    pub flags: u_char,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub union C2RustUnnamed {
    pub offset: u_int,
    pub data: C2RustUnnamed_0,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct C2RustUnnamed_0 {
    pub attr: u_char,
    pub fg: u_char,
    pub bg: u_char,
    pub data: u_char,
}
pub type screen_cursor_style = u32;
pub const SCREEN_CURSOR_BAR: screen_cursor_style = 3;
pub const SCREEN_CURSOR_UNDERLINE: screen_cursor_style = 2;
pub const SCREEN_CURSOR_BLOCK: screen_cursor_style = 1;
pub const SCREEN_CURSOR_DEFAULT: screen_cursor_style = 0;
#[derive(Copy, Clone)]
#[repr(C)]
pub struct screen_titles {
    pub tqh_first: *mut screen_title_entry,
    pub tqh_last: *mut *mut screen_title_entry,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct screen_title_entry {
    pub text: *mut i8,
    pub entry: C2RustUnnamed_1,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct C2RustUnnamed_1 {
    pub tqe_next: *mut screen_title_entry,
    pub tqe_prev: *mut *mut screen_title_entry,
}
unsafe extern "C" fn screen_free_titles(mut s: *mut screen) {
    let mut title_entry: *mut screen_title_entry = std::ptr::null_mut::<screen_title_entry>();
    if ((*s).titles).is_null() {
        return;
    }
    loop {
        title_entry = (*(*s).titles).tqh_first;
        if title_entry.is_null() {
            break;
        }
        if !((*title_entry).entry.tqe_next).is_null() {
            (*(*title_entry).entry.tqe_next).entry.tqe_prev = (*title_entry).entry.tqe_prev;
        } else {
            (*(*s).titles).tqh_last = (*title_entry).entry.tqe_prev;
        }
        *(*title_entry).entry.tqe_prev = (*title_entry).entry.tqe_next;
        free((*title_entry).text as *mut libc::c_void);
        free(title_entry as *mut libc::c_void);
    }
    free((*s).titles as *mut libc::c_void);
    (*s).titles = std::ptr::null_mut::<screen_titles>();
}
#[no_mangle]
pub unsafe extern "C" fn screen_init(
    mut s: *mut screen,
    mut sx: u_int,
    mut sy: u_int,
    mut hlimit: u_int,
) {
    (*s).grid = grid_create(sx, sy, hlimit);
    (*s).saved_grid = std::ptr::null_mut::<grid>();
    (*s).title = xstrdup(b"\0" as *const u8 as *const i8);
    (*s).titles = std::ptr::null_mut::<screen_titles>();
    (*s).path = std::ptr::null_mut::<i8>();
    (*s).cstyle = SCREEN_CURSOR_DEFAULT;
    (*s).default_cstyle = SCREEN_CURSOR_DEFAULT;
    (*s).mode = 0x1 as i32;
    (*s).default_mode = 0 as i32;
    (*s).ccolour = -(1 as i32);
    (*s).default_ccolour = -(1 as i32);
    (*s).tabs = std::ptr::null_mut::<bitstr_t>();
    (*s).sel = std::ptr::null_mut::<screen_sel>();
    (*s).write_list = std::ptr::null_mut::<screen_write_cline>();
    (*s).hyperlinks = std::ptr::null_mut::<hyperlinks>();
    screen_reinit(s);
}
#[no_mangle]
pub unsafe extern "C" fn screen_reinit(mut s: *mut screen) {
    (*s).cx = 0 as i32 as u_int;
    (*s).cy = 0 as i32 as u_int;
    (*s).rupper = 0 as i32 as u_int;
    (*s).rlower = ((*(*s).grid).sy).wrapping_sub(1 as i32 as u32);
    (*s).mode = 0x1 as i32 | 0x10 as i32 | (*s).mode & 0x4000 as i32;
    if options_get_number(
        global_options,
        b"extended-keys\0" as *const u8 as *const i8,
    ) == 2 as i32 as i64
    {
        (*s).mode =
            (*s).mode & !(0x8000 as i32 | 0x40000 as i32) | 0x8000 as i32;
    }
    if !((*s).saved_grid).is_null() {
        screen_alternate_off(s, std::ptr::null_mut::<grid_cell>(), 0 as i32);
    }
    (*s).saved_cx = (2147483647 as i32 as u32)
        .wrapping_mul(2 as u32)
        .wrapping_add(1 as u32);
    (*s).saved_cy = (2147483647 as i32 as u32)
        .wrapping_mul(2 as u32)
        .wrapping_add(1 as u32);
    screen_reset_tabs(s);
    grid_clear_lines(
        (*s).grid,
        (*(*s).grid).hsize,
        (*(*s).grid).sy,
        8 as i32 as u_int,
    );
    screen_clear_selection(s);
    screen_free_titles(s);
    screen_reset_hyperlinks(s);
}
#[no_mangle]
pub unsafe extern "C" fn screen_reset_hyperlinks(mut s: *mut screen) {
    if ((*s).hyperlinks).is_null() {
        (*s).hyperlinks = hyperlinks_init();
    } else {
        hyperlinks_reset((*s).hyperlinks);
    };
}
#[no_mangle]
pub unsafe extern "C" fn screen_free(mut s: *mut screen) {
    free((*s).sel as *mut libc::c_void);
    free((*s).tabs as *mut libc::c_void);
    free((*s).path as *mut libc::c_void);
    free((*s).title as *mut libc::c_void);
    if !((*s).write_list).is_null() {
        screen_write_free_list(s);
    }
    if !((*s).saved_grid).is_null() {
        grid_destroy((*s).saved_grid);
    }
    grid_destroy((*s).grid);
    if !((*s).hyperlinks).is_null() {
        hyperlinks_free((*s).hyperlinks);
    }
    screen_free_titles(s);
}
#[no_mangle]
pub unsafe extern "C" fn screen_reset_tabs(mut s: *mut screen) {
    let mut i: u_int = 0;
    free((*s).tabs as *mut libc::c_void);
    (*s).tabs = calloc(
        (((*(*s).grid).sx).wrapping_add(7 as i32 as u32) >> 3 as i32)
            as size_t,
        ::core::mem::size_of::<bitstr_t>() as u64,
    ) as *mut bitstr_t;
    if ((*s).tabs).is_null() {
        fatal(b"bit_alloc failed\0" as *const u8 as *const i8);
    }
    i = 8 as i32 as u_int;
    while i < (*(*s).grid).sx {
        let fresh0 = &mut (*((*s).tabs).offset((i >> 3 as i32) as isize));
        *fresh0 = (*fresh0 as i32
            | (1 as i32) << (i & 0x7 as i32 as u32))
            as bitstr_t;
        i = (i as u32).wrapping_add(8 as i32 as u32) as u_int as u_int;
    }
}
#[no_mangle]
pub unsafe extern "C" fn screen_set_default_cursor(mut s: *mut screen, mut oo: *mut options) {
    let mut c: i32 = 0;
    c = options_get_number(oo, b"cursor-colour\0" as *const u8 as *const i8)
        as i32;
    (*s).default_ccolour = c;
    c = options_get_number(oo, b"cursor-style\0" as *const u8 as *const i8)
        as i32;
    (*s).default_mode = 0 as i32;
    screen_set_cursor_style(c as u_int, &mut (*s).default_cstyle, &mut (*s).default_mode);
}
#[no_mangle]
pub unsafe extern "C" fn screen_set_cursor_style(
    mut style: u_int,
    mut cstyle: *mut screen_cursor_style,
    mut mode: *mut i32,
) {
    match style {
        0 => {
            *cstyle = SCREEN_CURSOR_DEFAULT;
        }
        1 => {
            *cstyle = SCREEN_CURSOR_BLOCK;
            *mode |= 0x80 as i32;
        }
        2 => {
            *cstyle = SCREEN_CURSOR_BLOCK;
            *mode &= !(0x80 as i32);
        }
        3 => {
            *cstyle = SCREEN_CURSOR_UNDERLINE;
            *mode |= 0x80 as i32;
        }
        4 => {
            *cstyle = SCREEN_CURSOR_UNDERLINE;
            *mode &= !(0x80 as i32);
        }
        5 => {
            *cstyle = SCREEN_CURSOR_BAR;
            *mode |= 0x80 as i32;
        }
        6 => {
            *cstyle = SCREEN_CURSOR_BAR;
            *mode &= !(0x80 as i32);
        }
        _ => {}
    };
}
#[no_mangle]
pub unsafe extern "C" fn screen_set_cursor_colour(mut s: *mut screen, mut colour: i32) {
    (*s).ccolour = colour;
}
#[no_mangle]
pub unsafe extern "C" fn screen_set_title(
    mut s: *mut screen,
    mut title: *const i8,
) -> i32 {
    if utf8_isvalid(title) == 0 {
        return 0 as i32;
    }
    free((*s).title as *mut libc::c_void);
    (*s).title = xstrdup(title);
    return 1 as i32;
}
#[no_mangle]
pub unsafe extern "C" fn screen_set_path(mut s: *mut screen, mut path: *const i8) {
    free((*s).path as *mut libc::c_void);
    utf8_stravis(
        &mut (*s).path,
        path,
        0x1 as i32 | 0x2 as i32 | 0x8 as i32 | 0x10 as i32,
    );
}
#[no_mangle]
pub unsafe extern "C" fn screen_push_title(mut s: *mut screen) {
    let mut title_entry: *mut screen_title_entry = std::ptr::null_mut::<screen_title_entry>();
    if ((*s).titles).is_null() {
        (*s).titles =
            xmalloc(::core::mem::size_of::<screen_titles>() as u64) as *mut screen_titles;
        (*(*s).titles).tqh_first = std::ptr::null_mut::<screen_title_entry>();
        (*(*s).titles).tqh_last = &mut (*(*s).titles).tqh_first;
    }
    title_entry = xmalloc(::core::mem::size_of::<screen_title_entry>() as u64)
        as *mut screen_title_entry;
    (*title_entry).text = xstrdup((*s).title);
    (*title_entry).entry.tqe_next = (*(*s).titles).tqh_first;
    if !((*title_entry).entry.tqe_next).is_null() {
        (*(*(*s).titles).tqh_first).entry.tqe_prev = &mut (*title_entry).entry.tqe_next;
    } else {
        (*(*s).titles).tqh_last = &mut (*title_entry).entry.tqe_next;
    }
    (*(*s).titles).tqh_first = title_entry;
    (*title_entry).entry.tqe_prev = &mut (*(*s).titles).tqh_first;
}
#[no_mangle]
pub unsafe extern "C" fn screen_pop_title(mut s: *mut screen) {
    let mut title_entry: *mut screen_title_entry = std::ptr::null_mut::<screen_title_entry>();
    if ((*s).titles).is_null() {
        return;
    }
    title_entry = (*(*s).titles).tqh_first;
    if !title_entry.is_null() {
        screen_set_title(s, (*title_entry).text);
        if !((*title_entry).entry.tqe_next).is_null() {
            (*(*title_entry).entry.tqe_next).entry.tqe_prev = (*title_entry).entry.tqe_prev;
        } else {
            (*(*s).titles).tqh_last = (*title_entry).entry.tqe_prev;
        }
        *(*title_entry).entry.tqe_prev = (*title_entry).entry.tqe_next;
        free((*title_entry).text as *mut libc::c_void);
        free(title_entry as *mut libc::c_void);
    }
}
#[no_mangle]
pub unsafe extern "C" fn screen_resize_cursor(
    mut s: *mut screen,
    mut sx: u_int,
    mut sy: u_int,
    mut reflow: i32,
    mut eat_empty: i32,
    mut cursor: i32,
) {
    let mut cx: u_int = (*s).cx;
    let mut cy: u_int = ((*(*s).grid).hsize).wrapping_add((*s).cy);
    if !((*s).write_list).is_null() {
        screen_write_free_list(s);
    }
    log_debug(
        b"%s: new size %ux%u, now %ux%u (cursor %u,%u = %u,%u)\0" as *const u8
            as *const i8,
        (*::core::mem::transmute::<&[u8; 21], &[i8; 21]>(b"screen_resize_cursor\0"))
            .as_ptr(),
        sx,
        sy,
        (*(*s).grid).sx,
        (*(*s).grid).sy,
        (*s).cx,
        (*s).cy,
        cx,
        cy,
    );
    if sx < 1 as i32 as u32 {
        sx = 1 as i32 as u_int;
    }
    if sy < 1 as i32 as u32 {
        sy = 1 as i32 as u_int;
    }
    if sx != (*(*s).grid).sx {
        (*(*s).grid).sx = sx;
        screen_reset_tabs(s);
    } else {
        reflow = 0 as i32;
    }
    if sy != (*(*s).grid).sy {
        screen_resize_y(s, sy, eat_empty, &mut cy);
    }
    if reflow != 0 {
        screen_reflow(s, sx, &mut cx, &mut cy, cursor);
    }
    if cy >= (*(*s).grid).hsize {
        (*s).cx = cx;
        (*s).cy = cy.wrapping_sub((*(*s).grid).hsize);
    } else {
        (*s).cx = 0 as i32 as u_int;
        (*s).cy = 0 as i32 as u_int;
    }
    log_debug(
        b"%s: cursor finished at %u,%u = %u,%u\0" as *const u8 as *const i8,
        (*::core::mem::transmute::<&[u8; 21], &[i8; 21]>(b"screen_resize_cursor\0"))
            .as_ptr(),
        (*s).cx,
        (*s).cy,
        cx,
        cy,
    );
    if !((*s).write_list).is_null() {
        screen_write_make_list(s);
    }
}
#[no_mangle]
pub unsafe extern "C" fn screen_resize(
    mut s: *mut screen,
    mut sx: u_int,
    mut sy: u_int,
    mut reflow: i32,
) {
    screen_resize_cursor(s, sx, sy, reflow, 1 as i32, 1 as i32);
}
unsafe extern "C" fn screen_resize_y(
    mut s: *mut screen,
    mut sy: u_int,
    mut eat_empty: i32,
    mut cy: *mut u_int,
) {
    let mut gd: *mut grid = (*s).grid;
    let mut needed: u_int = 0;
    let mut available: u_int = 0;
    let mut oldy: u_int = 0;
    let mut i: u_int = 0;
    if sy == 0 as i32 as u32 {
        fatalx(b"zero size\0" as *const u8 as *const i8);
    }
    oldy = (*(*s).grid).sy;
    if sy < oldy {
        needed = oldy.wrapping_sub(sy);
        if eat_empty != 0 {
            available = oldy
                .wrapping_sub(1 as i32 as u32)
                .wrapping_sub((*s).cy);
            if available > 0 as i32 as u32 {
                if available > needed {
                    available = needed;
                }
                grid_view_delete_lines(
                    gd,
                    oldy.wrapping_sub(available),
                    available,
                    8 as i32 as u_int,
                );
            }
            needed = (needed as u32).wrapping_sub(available) as u_int as u_int;
        }
        available = (*s).cy;
        if (*gd).flags & 0x1 as i32 != 0 {
            (*gd).hscrolled =
                ((*gd).hscrolled as u32).wrapping_add(needed) as u_int as u_int;
            (*gd).hsize = ((*gd).hsize as u32).wrapping_add(needed) as u_int as u_int;
        } else if needed > 0 as i32 as u32
            && available > 0 as i32 as u32
        {
            if available > needed {
                available = needed;
            }
            grid_view_delete_lines(
                gd,
                0 as i32 as u_int,
                available,
                8 as i32 as u_int,
            );
            *cy = (*cy as u32).wrapping_sub(available) as u_int as u_int;
        }
    }
    grid_adjust_lines(gd, ((*gd).hsize).wrapping_add(sy));
    if sy > oldy {
        needed = sy.wrapping_sub(oldy);
        available = (*gd).hscrolled;
        if (*gd).flags & 0x1 as i32 != 0 && available > 0 as i32 as u32 {
            if available > needed {
                available = needed;
            }
            (*gd).hscrolled =
                ((*gd).hscrolled as u32).wrapping_sub(available) as u_int as u_int;
            (*gd).hsize = ((*gd).hsize as u32).wrapping_sub(available) as u_int as u_int;
        } else {
            available = 0 as i32 as u_int;
        }
        needed = (needed as u32).wrapping_sub(available) as u_int as u_int;
        i = ((*gd).hsize).wrapping_add(sy).wrapping_sub(needed);
        while i < ((*gd).hsize).wrapping_add(sy) {
            grid_empty_line(gd, i, 8 as i32 as u_int);
            i = i.wrapping_add(1);
        }
    }
    (*gd).sy = sy;
    (*s).rupper = 0 as i32 as u_int;
    (*s).rlower = ((*(*s).grid).sy).wrapping_sub(1 as i32 as u32);
}
#[no_mangle]
pub unsafe extern "C" fn screen_set_selection(
    mut s: *mut screen,
    mut sx: u_int,
    mut sy: u_int,
    mut ex: u_int,
    mut ey: u_int,
    mut rectangle: u_int,
    mut modekeys: i32,
    mut gc: *mut grid_cell,
) {
    if ((*s).sel).is_null() {
        (*s).sel = xcalloc(
            1 as i32 as size_t,
            ::core::mem::size_of::<screen_sel>() as u64,
        ) as *mut screen_sel;
    }
    memcpy(
        &mut (*(*s).sel).cell as *mut grid_cell as *mut libc::c_void,
        gc as *const libc::c_void,
        ::core::mem::size_of::<grid_cell>() as u64,
    );
    (*(*s).sel).hidden = 0 as i32;
    (*(*s).sel).rectangle = rectangle as i32;
    (*(*s).sel).modekeys = modekeys;
    (*(*s).sel).sx = sx;
    (*(*s).sel).sy = sy;
    (*(*s).sel).ex = ex;
    (*(*s).sel).ey = ey;
}
#[no_mangle]
pub unsafe extern "C" fn screen_clear_selection(mut s: *mut screen) {
    free((*s).sel as *mut libc::c_void);
    (*s).sel = std::ptr::null_mut::<screen_sel>();
}
#[no_mangle]
pub unsafe extern "C" fn screen_hide_selection(mut s: *mut screen) {
    if !((*s).sel).is_null() {
        (*(*s).sel).hidden = 1 as i32;
    }
}
#[no_mangle]
pub unsafe extern "C" fn screen_check_selection(
    mut s: *mut screen,
    mut px: u_int,
    mut py: u_int,
) -> i32 {
    let mut sel: *mut screen_sel = (*s).sel;
    let mut xx: u_int = 0;
    if sel.is_null() || (*sel).hidden != 0 {
        return 0 as i32;
    }
    if (*sel).rectangle != 0 {
        if (*sel).sy < (*sel).ey {
            if py < (*sel).sy || py > (*sel).ey {
                return 0 as i32;
            }
        } else if (*sel).sy > (*sel).ey {
            if py > (*sel).sy || py < (*sel).ey {
                return 0 as i32;
            }
        } else if py != (*sel).sy {
            return 0 as i32;
        }
        if (*sel).ex < (*sel).sx {
            if px < (*sel).ex {
                return 0 as i32;
            }
            if px > (*sel).sx {
                return 0 as i32;
            }
        } else {
            if px < (*sel).sx {
                return 0 as i32;
            }
            if px > (*sel).ex {
                return 0 as i32;
            }
        }
    } else if (*sel).sy < (*sel).ey {
        if py < (*sel).sy || py > (*sel).ey {
            return 0 as i32;
        }
        if py == (*sel).sy && px < (*sel).sx {
            return 0 as i32;
        }
        if (*sel).modekeys == 0 as i32 {
            xx = if (*sel).ex == 0 as i32 as u32 {
                0 as i32 as u32
            } else {
                ((*sel).ex).wrapping_sub(1 as i32 as u32)
            };
        } else {
            xx = (*sel).ex;
        }
        if py == (*sel).ey && px > xx {
            return 0 as i32;
        }
    } else if (*sel).sy > (*sel).ey {
        if py > (*sel).sy || py < (*sel).ey {
            return 0 as i32;
        }
        if py == (*sel).ey && px < (*sel).ex {
            return 0 as i32;
        }
        if (*sel).modekeys == 0 as i32 {
            xx = ((*sel).sx).wrapping_sub(1 as i32 as u32);
        } else {
            xx = (*sel).sx;
        }
        if py == (*sel).sy && ((*sel).sx == 0 as i32 as u32 || px > xx) {
            return 0 as i32;
        }
    } else {
        if py != (*sel).sy {
            return 0 as i32;
        }
        if (*sel).ex < (*sel).sx {
            if (*sel).modekeys == 0 as i32 {
                xx = ((*sel).sx).wrapping_sub(1 as i32 as u32);
            } else {
                xx = (*sel).sx;
            }
            if px > xx || px < (*sel).ex {
                return 0 as i32;
            }
        } else {
            if (*sel).modekeys == 0 as i32 {
                xx = if (*sel).ex == 0 as i32 as u32 {
                    0 as i32 as u32
                } else {
                    ((*sel).ex).wrapping_sub(1 as i32 as u32)
                };
            } else {
                xx = (*sel).ex;
            }
            if px < (*sel).sx || px > xx {
                return 0 as i32;
            }
        }
    }
    return 1 as i32;
}
#[no_mangle]
pub unsafe extern "C" fn screen_select_cell(
    mut s: *mut screen,
    mut dst: *mut grid_cell,
    mut src: *const grid_cell,
) {
    if ((*s).sel).is_null() || (*(*s).sel).hidden != 0 {
        return;
    }
    memcpy(
        dst as *mut libc::c_void,
        &mut (*(*s).sel).cell as *mut grid_cell as *const libc::c_void,
        ::core::mem::size_of::<grid_cell>() as u64,
    );
    utf8_copy(&mut (*dst).data, &(*src).data);
    (*dst).attr = ((*dst).attr as i32 & !(0x80 as i32)) as u_short;
    (*dst).attr =
        ((*dst).attr as i32 | (*src).attr as i32 & 0x80 as i32) as u_short;
    (*dst).flags = (*src).flags;
}
unsafe extern "C" fn screen_reflow(
    mut s: *mut screen,
    mut new_x: u_int,
    mut cx: *mut u_int,
    mut cy: *mut u_int,
    mut cursor: i32,
) {
    let mut wx: u_int = 0;
    let mut wy: u_int = 0;
    if cursor != 0 {
        grid_wrap_position((*s).grid, *cx, *cy, &mut wx, &mut wy);
        log_debug(
            b"%s: cursor %u,%u is %u,%u\0" as *const u8 as *const i8,
            (*::core::mem::transmute::<&[u8; 14], &[i8; 14]>(b"screen_reflow\0"))
                .as_ptr(),
            *cx,
            *cy,
            wx,
            wy,
        );
    }
    grid_reflow((*s).grid, new_x);
    if cursor != 0 {
        grid_unwrap_position((*s).grid, cx, cy, wx, wy);
        log_debug(
            b"%s: new cursor is %u,%u\0" as *const u8 as *const i8,
            (*::core::mem::transmute::<&[u8; 14], &[i8; 14]>(b"screen_reflow\0"))
                .as_ptr(),
            *cx,
            *cy,
        );
    } else {
        *cx = 0 as i32 as u_int;
        *cy = (*(*s).grid).hsize;
    };
}
#[no_mangle]
pub unsafe extern "C" fn screen_alternate_on(
    mut s: *mut screen,
    mut gc: *mut grid_cell,
    mut cursor: i32,
) {
    let mut sx: u_int = 0;
    let mut sy: u_int = 0;
    if !((*s).saved_grid).is_null() {
        return;
    }
    sx = (*(*s).grid).sx;
    sy = (*(*s).grid).sy;
    (*s).saved_grid = grid_create(sx, sy, 0 as i32 as u_int);
    grid_duplicate_lines(
        (*s).saved_grid,
        0 as i32 as u_int,
        (*s).grid,
        (*(*s).grid).hsize,
        sy,
    );
    if cursor != 0 {
        (*s).saved_cx = (*s).cx;
        (*s).saved_cy = (*s).cy;
    }
    memcpy(
        &mut (*s).saved_cell as *mut grid_cell as *mut libc::c_void,
        gc as *const libc::c_void,
        ::core::mem::size_of::<grid_cell>() as u64,
    );
    grid_view_clear(
        (*s).grid,
        0 as i32 as u_int,
        0 as i32 as u_int,
        sx,
        sy,
        8 as i32 as u_int,
    );
    (*s).saved_flags = (*(*s).grid).flags;
    (*(*s).grid).flags &= !(0x1 as i32);
}
#[no_mangle]
pub unsafe extern "C" fn screen_alternate_off(
    mut s: *mut screen,
    mut gc: *mut grid_cell,
    mut cursor: i32,
) {
    let mut sx: u_int = (*(*s).grid).sx;
    let mut sy: u_int = (*(*s).grid).sy;
    if !((*s).saved_grid).is_null() {
        screen_resize(
            s,
            (*(*s).saved_grid).sx,
            (*(*s).saved_grid).sy,
            0 as i32,
        );
    }
    if cursor != 0
        && (*s).saved_cx
            != (2147483647 as i32 as u32)
                .wrapping_mul(2 as u32)
                .wrapping_add(1 as u32)
        && (*s).saved_cy
            != (2147483647 as i32 as u32)
                .wrapping_mul(2 as u32)
                .wrapping_add(1 as u32)
    {
        (*s).cx = (*s).saved_cx;
        (*s).cy = (*s).saved_cy;
        if !gc.is_null() {
            memcpy(
                gc as *mut libc::c_void,
                &mut (*s).saved_cell as *mut grid_cell as *const libc::c_void,
                ::core::mem::size_of::<grid_cell>() as u64,
            );
        }
    }
    if ((*s).saved_grid).is_null() {
        if (*s).cx > ((*(*s).grid).sx).wrapping_sub(1 as i32 as u32) {
            (*s).cx = ((*(*s).grid).sx).wrapping_sub(1 as i32 as u32);
        }
        if (*s).cy > ((*(*s).grid).sy).wrapping_sub(1 as i32 as u32) {
            (*s).cy = ((*(*s).grid).sy).wrapping_sub(1 as i32 as u32);
        }
        return;
    }
    grid_duplicate_lines(
        (*s).grid,
        (*(*s).grid).hsize,
        (*s).saved_grid,
        0 as i32 as u_int,
        (*(*s).saved_grid).sy,
    );
    if (*s).saved_flags & 0x1 as i32 != 0 {
        (*(*s).grid).flags |= 0x1 as i32;
    }
    screen_resize(s, sx, sy, 1 as i32);
    grid_destroy((*s).saved_grid);
    (*s).saved_grid = std::ptr::null_mut::<grid>();
    if (*s).cx > ((*(*s).grid).sx).wrapping_sub(1 as i32 as u32) {
        (*s).cx = ((*(*s).grid).sx).wrapping_sub(1 as i32 as u32);
    }
    if (*s).cy > ((*(*s).grid).sy).wrapping_sub(1 as i32 as u32) {
        (*s).cy = ((*(*s).grid).sy).wrapping_sub(1 as i32 as u32);
    }
}
#[no_mangle]
pub unsafe extern "C" fn screen_mode_to_string(mut mode: i32) -> *const i8 {
    static mut tmp: [i8; 1024] = [0; 1024];
    if mode == 0 as i32 {
        return b"NONE\0" as *const u8 as *const i8;
    }
    if mode == 0xffffff as i32 {
        return b"ALL\0" as *const u8 as *const i8;
    }
    *tmp.as_mut_ptr() = '\0' as i32 as i8;
    if mode & 0x1 as i32 != 0 {
        strlcat(
            tmp.as_mut_ptr(),
            b"CURSOR,\0" as *const u8 as *const i8,
            ::core::mem::size_of::<[i8; 1024]>() as u64,
        );
    }
    if mode & 0x2 as i32 != 0 {
        strlcat(
            tmp.as_mut_ptr(),
            b"INSERT,\0" as *const u8 as *const i8,
            ::core::mem::size_of::<[i8; 1024]>() as u64,
        );
    }
    if mode & 0x4 as i32 != 0 {
        strlcat(
            tmp.as_mut_ptr(),
            b"KCURSOR,\0" as *const u8 as *const i8,
            ::core::mem::size_of::<[i8; 1024]>() as u64,
        );
    }
    if mode & 0x8 as i32 != 0 {
        strlcat(
            tmp.as_mut_ptr(),
            b"KKEYPAD,\0" as *const u8 as *const i8,
            ::core::mem::size_of::<[i8; 1024]>() as u64,
        );
    }
    if mode & 0x10 as i32 != 0 {
        strlcat(
            tmp.as_mut_ptr(),
            b"WRAP,\0" as *const u8 as *const i8,
            ::core::mem::size_of::<[i8; 1024]>() as u64,
        );
    }
    if mode & 0x20 as i32 != 0 {
        strlcat(
            tmp.as_mut_ptr(),
            b"MOUSE_STANDARD,\0" as *const u8 as *const i8,
            ::core::mem::size_of::<[i8; 1024]>() as u64,
        );
    }
    if mode & 0x40 as i32 != 0 {
        strlcat(
            tmp.as_mut_ptr(),
            b"MOUSE_BUTTON,\0" as *const u8 as *const i8,
            ::core::mem::size_of::<[i8; 1024]>() as u64,
        );
    }
    if mode & 0x80 as i32 != 0 {
        strlcat(
            tmp.as_mut_ptr(),
            b"CURSOR_BLINKING,\0" as *const u8 as *const i8,
            ::core::mem::size_of::<[i8; 1024]>() as u64,
        );
    }
    if mode & 0x10000 as i32 != 0 {
        strlcat(
            tmp.as_mut_ptr(),
            b"CURSOR_VERY_VISIBLE,\0" as *const u8 as *const i8,
            ::core::mem::size_of::<[i8; 1024]>() as u64,
        );
    }
    if mode & 0x100 as i32 != 0 {
        strlcat(
            tmp.as_mut_ptr(),
            b"MOUSE_UTF8,\0" as *const u8 as *const i8,
            ::core::mem::size_of::<[i8; 1024]>() as u64,
        );
    }
    if mode & 0x200 as i32 != 0 {
        strlcat(
            tmp.as_mut_ptr(),
            b"MOUSE_SGR,\0" as *const u8 as *const i8,
            ::core::mem::size_of::<[i8; 1024]>() as u64,
        );
    }
    if mode & 0x400 as i32 != 0 {
        strlcat(
            tmp.as_mut_ptr(),
            b"BRACKETPASTE,\0" as *const u8 as *const i8,
            ::core::mem::size_of::<[i8; 1024]>() as u64,
        );
    }
    if mode & 0x800 as i32 != 0 {
        strlcat(
            tmp.as_mut_ptr(),
            b"FOCUSON,\0" as *const u8 as *const i8,
            ::core::mem::size_of::<[i8; 1024]>() as u64,
        );
    }
    if mode & 0x1000 as i32 != 0 {
        strlcat(
            tmp.as_mut_ptr(),
            b"MOUSE_ALL,\0" as *const u8 as *const i8,
            ::core::mem::size_of::<[i8; 1024]>() as u64,
        );
    }
    if mode & 0x2000 as i32 != 0 {
        strlcat(
            tmp.as_mut_ptr(),
            b"ORIGIN,\0" as *const u8 as *const i8,
            ::core::mem::size_of::<[i8; 1024]>() as u64,
        );
    }
    if mode & 0x4000 as i32 != 0 {
        strlcat(
            tmp.as_mut_ptr(),
            b"CRLF,\0" as *const u8 as *const i8,
            ::core::mem::size_of::<[i8; 1024]>() as u64,
        );
    }
    if mode & 0x8000 as i32 != 0 {
        strlcat(
            tmp.as_mut_ptr(),
            b"KEYS_EXTENDED,\0" as *const u8 as *const i8,
            ::core::mem::size_of::<[i8; 1024]>() as u64,
        );
    }
    if mode & 0x40000 as i32 != 0 {
        strlcat(
            tmp.as_mut_ptr(),
            b"KEYS_EXTENDED_2,\0" as *const u8 as *const i8,
            ::core::mem::size_of::<[i8; 1024]>() as u64,
        );
    }
    tmp[(strlen(tmp.as_mut_ptr())).wrapping_sub(1 as i32 as u64) as usize] =
        '\0' as i32 as i8;
    return tmp.as_mut_ptr();
}
